package com.example.gallery;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.util.Log;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.board.R;


@SuppressLint("NewApi")
public class BoardListActivity extends android.app.ListActivity implements OnDragListener,OnClickListener, OnItemClickListener,OnItemLongClickListener, OnTouchListener{
	
	static final String TAG = "ListActivity";
	BoardHandler boardDB = null;
	int no;
	String ID;
	String PW;
	
	ListView lv_list;
	ArrayList<BoardDTO> list;
	Button btn_insert;
	Button btn_camera;
	Button btn_logout;
	TextView tv_date;
	
	private boolean m_Flag = false;
	Handler back_Handler = new Handler(new Callback() {
		public boolean handleMessage(Message msg) {
			if(msg.what == 0) {
				m_Flag = false;
			}
			return true;
		}
	});
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.board_list);
		Log.i(TAG, "onCreate");
//		View v = (View) findViewById(R.layout.row);
//		tv_date = (TextView)v.findViewById(R.id.tv_date);
		btn_insert = (Button) findViewById(R.id.btn_insert);
		btn_logout = (Button) findViewById(R.id.btn_logout);
		
		lv_list = (ListView)findViewById(android.R.id.list);
		
//		tv_date.setSelected(true);
		
		btn_insert.setOnClickListener(this);
		btn_logout.setOnClickListener(this);
		lv_list.setOnItemClickListener(this);
//		lv_list.onTouchEvent(this);
		lv_list.setOnItemLongClickListener(this);
		
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.i(TAG, "onResume");

		list = new ArrayList<BoardDTO>();
		
		BoardHandler db = BoardHandler.open(this);
		list = db.getList();
		
		BoardAdapter adapter = new BoardAdapter(this, R.layout.row, list);
		adapter.notifyDataSetChanged();
		lv_list.invalidate();
		lv_list.setAdapter(adapter);
		lv_list.invalidate();
		adapter.notifyDataSetChanged();
	
	}

	@Override
	public boolean onDrag(View v, DragEvent event) {
		return false;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		BoardDTO dto =  (BoardDTO) parent.getAdapter().getItem(position);
		no = dto.getNO();	//�Խñ� ��ȣ
		Intent intent = new Intent(this, DetailActivity.class);
		intent.putExtra("no", String.valueOf(no));	//�Խñ� ��ȣ 
		startActivity(intent);
		finish();
//		Toast.makeText(getApplicationContext(),"�۹�ȣ : "+no, Toast.LENGTH_SHORT).show();
		
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		final BoardDTO board = (BoardDTO) parent.getAdapter().getItem(position);
		no = board.getNO();
		Log.i(TAG, "������ �� ��ȣ : "+no);
		
		final BoardHandler db = BoardHandler.open(this);

		AlertDialog.Builder ad = new AlertDialog.Builder(this);
		ad.setTitle("����");
		ad.setMessage(board.getTITLE()+" �����Ͻðڽ��ϱ�?");
		
		ad.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				SharedPreferences prf = getSharedPreferences("login", 0);
				ID = prf.getString("id",	"");
				PW = prf.getString("PW", "");
				if(board.getID().equals(ID)){
					int succ = db.delete(no);
					if(succ > 0){
						Log.i(TAG, "������ �۹�ȣ : "+no);
						onResume();
					}
					else{
						Toast.makeText(getApplicationContext(), "�� ���� ����", Toast.LENGTH_SHORT).show();
						onResume();
					}
				}
				else{
					Toast.makeText(getApplicationContext(), "���� �Խñ۸� ���� �� �� �ֽ��ϴ�", Toast.LENGTH_LONG).show();
				}
			}
		}).setNegativeButton("No", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				onResume();
			}
		});
		AlertDialog dialog = ad.create();
		dialog.show();
		return true;
	}
	@Override
	public void onClick(View v) {
		boardDB = BoardHandler.open(this);
		switch (v.getId()) {
		
		case R.id.btn_insert:
			Intent intent = new Intent(this, InsertActivity.class);
			int Board_NO = boardDB.getNO();
			Bundle b = new Bundle();
			b.putInt("BOARD_NO", Board_NO);
			intent.putExtras(b);
			startActivity(intent);
			finish();
			break;
		case R.id.btn_logout:
			SharedPreferences.Editor prf = getSharedPreferences("login_info", 0).edit();
			prf.remove("id");
			prf.remove("pw");
			prf.commit();
			Intent intent2 = new Intent(this, MainActivity.class);
			startActivity(intent2);
			finish();
			break;
		default:
			break;
		}
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			
			break;
		case MotionEvent.ACTION_UP:
			
			break;
		case MotionEvent.ACTION_MOVE:
			
			break;

		default:
			break;
		}
		return false;
	}
	public boolean onKeyDown(int KeyCode, KeyEvent event) {
//		super.onKeyDown(KeyCode, event);
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (KeyCode) {
			case KeyEvent.KEYCODE_BACK:
				if (!m_Flag) {
					Toast.makeText(this, "'뒤로' 버튼을 한번 더 누르시면 종료됩니다.",
							Toast.LENGTH_SHORT).show();
					// 버튼클릭시 true
					m_Flag = true;
					// Handler 호출 (2초 이후 back_Check 값 false)
					back_Handler.sendEmptyMessageDelayed(0, 2000);
					return false;
				} else {						
					try{
					}catch(Exception e){
						e.printStackTrace();
					}						
					moveTaskToBack(true);
					finish();
					return true;
				}
			}
			return false;
		}		
		return false;
	}	
	
	public void refresh(){
		
	}
}
